<template>
	<view style="display: flex;flex-direction: column;align-items: center;">
		<view style="width: 100%;height: 100%;text-align: center;font-weight: bold;margin: 10% 0 12rpx 0;">
			请长按二维码
		</view>
		<view style="width: 100%;height: 100%;text-align: center;font-weight: bold;margin: 0% 0 5% 0;">
			关注『致胜供应链』公众号
		</view>
		<image src="/static/zsgyl_qrcode.jpg" style="width: 500rpx;height: 500rpx;"></image>

		<view style="color: #ccc;font-size: 22rpx;margin: 80rpx 0 0 0;text-align: center;">©致胜供应链提供技术支持</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '', // 记录用户登录code
			}
		},

		onLoad(opts) {
			// 获取微信登录code
			const urlParams = new URLSearchParams(window.location.search)
			this.code = urlParams.get('code') // 获取参数名为code的值
		},

		onShow() {
			if (this.code) {
				this.getWxOpenid(this.code)
			}
		},

		methods: {

			// 接口:获取微信openid
			async getWxOpenid(code) {
				if (!code) {
					return
				}
				let that = this
				uni.showLoading({ title: '加载中..', mask: true, })
				let res = await that.$getWxOpenid({ code })
				if (res.code == 1) {
					uni.setStorageSync('openid', res.data.openid)
					that.checkUserFollow(res.data.openid, res.data.access_token, code)
				} else {
					uni.showToast({ title: res.msg, icon: 'none' })
				}
			},

			// 接口:判断用户是否关注公众号
			async checkUserFollow(openid, accessToken, code) {
				if (!openid) {
					return
				}

				let that = this
				uni.showLoading({ title: '加载中..', mask: true, })

				// 获取用户信息
				// let res0 = await that.$getSNSUserInfo({ openid, accessToken })
				// if (res0.code == 1) {
				// 	uni.setStorageSync('openid', openid)
				// 	uni.setStorageSync('nickname', res0.data.nickname)
				// 	uni.setStorageSync('headurl', res0.data.headimgurl)
				// }

				// 获取公众号关注状态
				let res = await that.$checkUserFollow({ openid })
				// uni.showModal({ title: '提示', content: `请求成功 = ${JSON.stringify(res)}`, success: (res) => {} })
				if (res.code == 1) {
					let subscribe = res.data.subscribe
					// setTimeout(() => {
					// 	uni.showModal({ title: '提示', content: `请求成功.subscribe = ${subscribe}`, success: (res) => {} })
					// }, 2000)
					if (subscribe == '0') {
						// 未关注
					} else {
						uni.setStorageSync('subscribe', subscribe)
						uni.setStorageSync('subscribe_expire', Date.now() + 10 * 60 * 1000)
						window.history.back()
					}

				} else {
					uni.showToast({ title: res.msg, icon: 'none' })

					uni.showModal({
						title: '提示',
						content: `请求失败 = ${JSON.stringify(res)}`,
						// showCancel: false,
						success: (res) => {
							if (res.confirm) {
								uni.setClipboardData({
									data: `${code}`,
									success: (res) => {
										uni.showToast({ icon: 'none', title: '已复制到剪贴板' })
									}
								})
							}
						}
					})

				}
			},

		}
	}
</script>

<style scoped>

</style>